package com.lst.servlet;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.PlanReqPara;
import com.lst.entity.response.PlanResPara;
import com.lst.model.OnlineCourse;
import com.lst.model.Plan;
import com.lst.model.PlanCourse;
import com.lst.service.OnlineCourseService;
import com.lst.service.PlanCourseService;
import com.lst.service.PlanService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;
import com.lst.utils.TrainingDay;


/**
 * @ClassName: PlanAddServlet
 * @Description: 重新制定训练计划接口
 * 
 */
public class PlanAddNewServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private PlanService planService = ctx.getBean(PlanService.class);
	
	private OnlineCourseService onlineCourseService = ctx.getBean(OnlineCourseService.class);
	
	private PlanCourseService planCourseService = ctx.getBean(PlanCourseService.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		PlanResPara resPara = new PlanResPara();

		try {
			PlanReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			Integer userid = Integer.valueOf(reqPara.getUserid());
			
			 Map<String,Object> rusultMap=new HashMap<String,Object>();
    		 rusultMap.put("createUser", userid);
    		 rusultMap.put("orderByClause", "md.createDate desc");
    		 List<Plan> planList=planService.queryByList(rusultMap);
    		 Plan plan=planList.get(0);
    		 
			Integer identityType = StringUtils.isEmpty(reqPara.getIdentityType()) ? plan.getIdentityType() : Integer.valueOf(reqPara.getIdentityType());				
			Integer trainType = StringUtils.isEmpty(reqPara.getTrainType()) ? plan.getTrainType() : Integer.valueOf(reqPara.getTrainType());	
			Integer kfClassType = StringUtils.isEmpty(reqPara.getKfClassType()) ? plan.getKfClassType() : Integer.valueOf(reqPara.getKfClassType());	
			String instrumentType = StringUtils.isEmpty(reqPara.getInstrumentType()) ? plan.getInstrumentType() : reqPara.getInstrumentType();	
			Integer runType = StringUtils.isEmpty(reqPara.getRunType()) ? plan.getRunType() : Integer.valueOf(reqPara.getRunType());
			Integer frequency = StringUtils.isEmpty(reqPara.getFrequency()) ? plan.getFrequency() : Integer.valueOf(reqPara.getFrequency());
			Integer height = StringUtils.isEmpty(reqPara.getHeight()) ? plan.getHeight() : Integer.valueOf(reqPara.getHeight());
			Integer weight = StringUtils.isEmpty(reqPara.getWeight()) ? plan.getWeight() : Integer.valueOf(reqPara.getWeight());
			Date startTime = StringUtils.isEmpty(reqPara.getStartTime()) ? startDate : DateUtil.parse(reqPara.getStartTime(),DateUtil.ALL_DATE_HORIZONTAL);
               

			
			//获得4周的训练日
			List<String> list1=TrainingDay.train(startTime,frequency);
			
			//结果集
			List<Map<String,Integer>> result = new ArrayList<>();
			
			//没器械、不包含训练跑，只有普通课程	
			if("0".equals(instrumentType) && runType==1){
				//训练课程
				List<OnlineCourse> listCommon=gainCommon(identityType,trainType,kfClassType);
				if(listCommon!=null&&listCommon.size()>0){//三个条件查到课程进行排课
					Map<String,Integer> map1 = new LinkedHashMap<String,Integer>();
					if(listCommon.size()>=list1.size()){//课程数目多余训练天数
						for(int i=0;i<list1.size();i++){
						   map1.put(list1.get(i),listCommon.get(i).getId());
						}
						result.add(map1);
					}else{//课程少于训练天数，部分课程重复
						int len=listCommon.size();
						for(int i=0;i<list1.size();i++){
							if(i<len){
								 map1.put(list1.get(i),listCommon.get(i).getId());
							}else{
								 map1.put(list1.get(i),listCommon.get(i%len).getId());
							}
						}
						 result.add(map1);
					}
				}
					
			}
			
			//普通课程+器械课程
			if(!"0".equals(instrumentType) && runType==1){
				//器械课程
				List<OnlineCourse> listQX=gainqx(instrumentType,identityType,trainType,kfClassType);
				double index1=listQX.size();//集合长度
				//普通训练课程
				List<OnlineCourse> listCommon=gainCommon(identityType,trainType,kfClassType);
				if(listCommon!=null&&listCommon.size()>0){
					double index2=listCommon.size();
					if(index1==0){//如果没找到相关器械，排普通课程
							Map<String,Integer> map1 = new LinkedHashMap<String,Integer>();
							if(listCommon.size()>=list1.size()){//课程数目多余训练天数
								for(int i=0;i<list1.size();i++){
								   map1.put(list1.get(i),listCommon.get(i).getId());
								}
								result.add(map1);
							}else{//课程少于训练天数，部分课程重复
								int len=listCommon.size();
								for(int i=0;i<list1.size();i++){
									if(i<len){
										 map1.put(list1.get(i),listCommon.get(i).getId());
									}else{
										 map1.put(list1.get(i),listCommon.get(i%len).getId());
									}
								}
								 result.add(map1);
							}
					}else{//有器械推荐课程
						//课程集合，包括普通课程和器械课程
						List<OnlineCourse> listTotal=new ArrayList<OnlineCourse>();
						listTotal.add(listCommon.get(0));
						double rate=index2/index1;
					    index2++;
					    int num1=1;
					    int num2=0;
						for(int k=1;k<(listQX.size()+listCommon.size());k++){
							if(index2/index1>=rate){
								index1++;
								listTotal.add(listQX.get(num2));
								num2++;
							}else{
								index2++;
								listTotal.add(listCommon.get(num1));
								num1++;
							}
						}
						Map<String,Integer> map1 = new LinkedHashMap<String,Integer>();
						if(listTotal.size()>=list1.size()){//课程数目多余训练天数
							for(int i=0;i<list1.size();i++){
							   map1.put(list1.get(i),listTotal.get(i).getId());
							}
							result.add(map1);
						}else{//课程少于训练天数，部分课程重复
							int len=listTotal.size();
							for(int i=0;i<list1.size();i++){
								if(i<len){
									 map1.put(list1.get(i),listTotal.get(i).getId());
								}else{
									 map1.put(list1.get(i),listTotal.get(i%len).getId());
								}
							}
							 result.add(map1);
						}
					}
				}
			}
			
			//普通课程+训练跑课程
			if("0".equals(instrumentType) && runType==2){
				//训练跑课程
				List<OnlineCourse> listRun=gainTrain(runType,identityType,trainType,kfClassType);
				double index1=listRun.size();//集合长度
				//普通训练课程
				List<OnlineCourse> listCommon=gainCommon(identityType,trainType,kfClassType);
				if(listCommon!=null&&listCommon.size()>0){
					double index2=listCommon.size();
					if(index1==0){//如果没找到符合条件的训练跑，排普通课程
							Map<String,Integer> map1 = new LinkedHashMap<String,Integer>();
							if(listCommon.size()>=list1.size()){//课程数目多余训练天数
								for(int i=0;i<list1.size();i++){
								   map1.put(list1.get(i),listCommon.get(i).getId());
								}
								result.add(map1);
							}else{//课程少于训练天数，部分课程重复
								int len=listCommon.size();
								for(int i=0;i<list1.size();i++){
									if(i<len){
										 map1.put(list1.get(i),listCommon.get(i).getId());
									}else{
										 map1.put(list1.get(i),listCommon.get(i%len).getId());
									}
								}
								 result.add(map1);
							}
					}else{//有训练跑推荐课程
						//课程集合，包括普通课程和训练跑课程
						List<OnlineCourse> listTotal=new ArrayList<OnlineCourse>();
						listTotal.add(listCommon.get(0));
						double rate=index2/index1;
					    index2++;
					    int num1=1;
					    int num2=0;
						for(int k=1;k<(listRun.size()+listCommon.size());k++){
							if(index2/index1>=rate){
								index1++;
								listTotal.add(listRun.get(num2));
								num2++;
							}else{
								index2++;
								listTotal.add(listCommon.get(num1));
								num1++;
							}
						}
						Map<String,Integer> map1 = new LinkedHashMap<String,Integer>();
						if(listTotal.size()>=list1.size()){//课程数目多余训练天数
							for(int i=0;i<list1.size();i++){
							   map1.put(list1.get(i),listTotal.get(i).getId());
							}
							result.add(map1);
						}else{//课程少于训练天数，部分课程重复
							int len=listTotal.size();
							for(int i=0;i<list1.size();i++){
								if(i<len){
									 map1.put(list1.get(i),listTotal.get(i).getId());
								}else{
									 map1.put(list1.get(i),listTotal.get(i%len).getId());
								}
							}
							 result.add(map1);
						}
					}
				}
			}
			
			//普通课程+训练跑+器械课程
			if(!"0".equals(instrumentType) && runType==2){
				//训练跑课程
				List<OnlineCourse> listRun=gainTrain(runType,identityType,trainType,kfClassType);
				double s1=listRun.size();//集合长度
				//器械课程
				List<OnlineCourse> listQX=gainqx(instrumentType,identityType,trainType,kfClassType);
				double s2=listQX.size();//集合长度
				double rate2=s2/s1;
				List<OnlineCourse> listTotal2=new ArrayList<OnlineCourse>();
				if(s1==0&&s2!=0){
					for(int j=0;j<listQX.size();j++){
						listTotal2.add(listQX.get(j));
					}
				}
				if(s1!=0&&s2==0){
					for(int j=0;j<listRun.size();j++){
						listTotal2.add(listRun.get(j));
					}
				}
				if(s1!=0&&s2!=0){
					listTotal2.add(listQX.get(0));
					s2++;
					int t1=0;
					int t2=1;
					for(int k=1;k<(listQX.size()+listRun.size());k++){
						if(s2/s1>=rate2){
							s1++;
							listTotal2.add(listRun.get(t1));
							t1++;
						}else{
							s2++;
							listTotal2.add(listQX.get(t2));
							t2++;
						}
					}
				}
				double index1=listTotal2.size();
				
				//普通训练课程
				List<OnlineCourse> listCommon=gainCommon(identityType,trainType,kfClassType);
				if(listCommon!=null&&listCommon.size()>0){
					double index2=listCommon.size();
					if(index1==0){//如果没找到符合条件的器械训练和训练跑，排普通课程
							Map<String,Integer> map1 = new LinkedHashMap<String,Integer>();
							if(listCommon.size()>=list1.size()){//课程数目多余训练天数
								for(int i=0;i<list1.size();i++){
								   map1.put(list1.get(i),listCommon.get(i).getId());
								}
								result.add(map1);
							}else{//课程少于训练天数，部分课程重复
								int len=listCommon.size();
								for(int i=0;i<list1.size();i++){
									if(i<len){
										 map1.put(list1.get(i),listCommon.get(i).getId());
									}else{
										 map1.put(list1.get(i),listCommon.get(i%len).getId());
									}
								}
								 result.add(map1);
							}
					}else{//有器械或者训练跑推荐课程
						//课程集合，包括普通课程和训练跑课程
						List<OnlineCourse> listTotal=new ArrayList<OnlineCourse>();
						listTotal.add(listCommon.get(0));
						double rate=index2/index1;
					    index2++;
					    int num1=1;
					    int num2=0;
						for(int k=1;k<(listTotal2.size()+listCommon.size());k++){
							if(index2/index1>=rate){
								index1++;
								listTotal.add(listTotal2.get(num2));
								num2++;
							}else{
								index2++;
								listTotal.add(listCommon.get(num1));
								num1++;
							}
						}
						Map<String,Integer> map1 = new LinkedHashMap<String,Integer>();
						if(listTotal.size()>=list1.size()){//课程数目多余训练天数
							for(int i=0;i<list1.size();i++){
							   map1.put(list1.get(i),listTotal.get(i).getId());
							}
							result.add(map1);
						}else{//课程少于训练天数，部分课程重复
							int len=listTotal.size();
							for(int i=0;i<list1.size();i++){
								if(i<len){
									 map1.put(list1.get(i),listTotal.get(i).getId());
								}else{
									 map1.put(list1.get(i),listTotal.get(i%len).getId());
								}
							}
							 result.add(map1);
						}
					}
				}
			}
			if(!result.isEmpty()){
				//先查询是否有未完成计划,如果有把原计划作废，从新制定
				Plan plan4=planService.selectByUser(userid);
				if(plan4!=null){
					planService.update(plan4);
				}
				
				   Plan plan2=new Plan();
				    plan2.setIsDelete(false);
					plan2.setIdentityType(identityType);
					plan2.setTrainType(trainType);
					plan2.setKfClassType(kfClassType);
					plan2.setInstrumentType(instrumentType);
					plan2.setRunType(runType);
					plan2.setFrequency(frequency);
					plan2.setHeight(height);
					plan2.setWeight(weight);
					plan2.setCreateDate(startDate);
					plan2.setStartTime(startTime);
					plan2.setStatus(1);
					plan2.setCreateUser(userid);
					plan2.setIsNotice(plan.getIsNotice());
					plan2.setNoticeTime(plan.getNoticeTime());
					
					planService.insert(plan2);
					
					//制定计划时把未完成的计划作废
					Map<String,Object> reMap=new HashMap<String, Object>();
					reMap.put("createUser", userid);
					reMap.put("isDelete", true);
					reMap.put("planStatus", 3);
				    planCourseService.updateByUser(reMap);
				    //查询计划id,给推荐课程打上标签
				//    Plan plan3=planService.selectByUser(userid);
					//把结果保存到数据库
					PlanCourse planCourse=new PlanCourse();
					for(Map<String,Integer> map2 : result){
					    	  for(String key : map2.keySet()){
					    			planCourse.setCreateUser(userid);
					    			planCourse.setCreateDate(startDate);
					    			planCourse.setIsDelete(false);
					    			planCourse.setPlanStatus(1);
					    			planCourse.setTrainDate(DateUtil.parseTry(key,DateUtil.ALL_DATE_HORIZONTAL));
					    			planCourse.setCourseId(map2.get(key));
					    			planCourse.setPlanId(plan2.getId());
					    			planCourseService.insert(planCourse);
					    	  }
					 }
					
					Map<String,Object> reMap2=new HashMap<String, Object>();
					reMap2.put("createUser", userid);
					reMap2.put("isDelete", false);
					reMap2.put("planStatus", 1);
					reMap2.put("orderByClause", "md.trainDate");
					List<PlanCourse> planCourseList=planCourseService.selectByUser(reMap2);
					if(planCourseList!=null&&planCourseList.size()>0){
						for(PlanCourse course:planCourseList){
							  Date date=course.getTrainDate();
		 					  Long trainDate2=date.getTime();
		 					  course.setTrainDate2(trainDate2);
						}
					}
					
					resPara.setPlanCourseList(planCourseList);
					resPara.setCode(CommCode.M_Y000000);
					resPara.setMessage(CommCode.M_Y000001);
					
			}else{
				resPara.setCode(CommCode.M_A000000);
				resPara.setMessage("未找到符合条件的计划，制定失败");
			}
			
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
	//		logger.info("PlanAddNewServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

	//		logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public PlanReqPara getReqPara(HttpServletRequest request) {

		PlanReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new PlanReqPara());

			reqPara = (PlanReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String identityType = request.getParameter("identityType");
			String trainType = request.getParameter("trainType");
			String kfClassType = request.getParameter("kfClassType");
			String instrumentType = request.getParameter("instrumentType");
			String runType = request.getParameter("runType");
			String frequency = request.getParameter("frequency");
			String startTime = request.getParameter("startTime");
			String height = request.getParameter("height");
			String weight = request.getParameter("weight");
	
			
//			logger.info("reqPara userid:" + userid);
//			logger.info("reqPara identityType: " + identityType);
//			logger.info("reqPara trainType: " + trainType);
//			logger.info("reqPara kfClassType:" + kfClassType);
//			logger.info("reqPara instrumentType:" + instrumentType);
//			logger.info("reqPara frequency:" + frequency);
//			logger.info("reqPara runType:" + runType);
//			logger.info("reqPara startTime:" + startTime);
//			logger.info("reqPara height:" + height);
//			logger.info("reqPara weight:" + weight);

	        reqPara.setUserid(userid);
	        reqPara.setIdentityType(identityType);
	        reqPara.setTrainType(trainType);
	        reqPara.setKfClassType(kfClassType);
	        reqPara.setInstrumentType(instrumentType);
	        reqPara.setRunType(runType);
	        reqPara.setFrequency(frequency);
	        reqPara.setStartTime(startTime);
	        reqPara.setHeight(height);
	        reqPara.setWeight(weight);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		PlanReqPara cdReqPara = (PlanReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

		 if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		} else {
			flag = true;
		}

		return  flag;
	}
	/**
	 * 获得器械课程
	 * @param instrumentType
	 * @param identityType
	 * @param trainType
	 * @param kfClassType
	 * @return
	 */
	private List<OnlineCourse> gainqx(String instrumentType,Integer identityType,Integer trainType,Integer kfClassType){
		List<OnlineCourse> listQX=new ArrayList<OnlineCourse>();
		if(instrumentType!="0"){
			String[] instrumentNum=instrumentType.split(",");
			if(instrumentNum.length==1){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("enabled", true);
				map.put("isDeleted", false);
				if(identityType==376){
				   map.put("identityType", "(oc.CourseNo='lv1' or oc.CourseNo='lv2') and oc.instrumentType!=0");
				   }else if(identityType==377){
				   map.put("identityType", "(oc.CourseNo='lv2' or oc.CourseNo='lv3' or oc.CourseNo='lv4') and oc.instrumentType!=0");	
				   }else{
				   map.put("identityType", "(oc.CourseNo='lv3' or oc.CourseNo='lv4' or oc.CourseNo='lv5') and oc.instrumentType!=0");
				}
				map.put("trainType", trainType);
				map.put("kfClassType", kfClassType);
				map.put("runType", 0);
				map.put("instrumentNum1", Integer.valueOf(instrumentNum[0]));
				map.put("orderByClause", "oc.CreateDate desc");
				List<OnlineCourse> coureseList=onlineCourseService.queryByInstrument(map);
				if(coureseList!=null&&coureseList.size()>0){
					listQX.addAll(coureseList);
				}else{
					Map<String, Object> map2 = new HashMap<String, Object>();
					map2.put("enabled", true);
					map2.put("isDeleted", false);
					if(identityType==376){
					   map2.put("identityType", "(oc.CourseNo='lv1' or oc.CourseNo='lv2') and oc.instrumentType!=0");
					   }else if(identityType==377){
					   map2.put("identityType", "(oc.CourseNo='lv2' or oc.CourseNo='lv3' or oc.CourseNo='lv4') and oc.instrumentType!=0");	
					   }else{
					   map2.put("identityType", "(oc.CourseNo='lv3' or oc.CourseNo='lv4' or oc.CourseNo='lv5') and oc.instrumentType!=0");
					}
					map2.put("runType", 0);
					map2.put("instrumentNum1", Integer.valueOf(instrumentNum[0]));
					map2.put("orderByClause", "oc.CreateDate desc");
					List<OnlineCourse> coureseList2=onlineCourseService.queryByInstrument(map2);
					listQX.addAll(coureseList2);
				}
				
			}else if(instrumentNum.length==2){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("enabled", true);
				map.put("isDeleted", false);
				if(identityType==376){
					   map.put("identityType", "(oc.CourseNo='lv1' or oc.CourseNo='lv2') and oc.instrumentType!=0");
					   }else if(identityType==377){
					   map.put("identityType", "(oc.CourseNo='lv2' or oc.CourseNo='lv3' or oc.CourseNo='lv4') and oc.instrumentType!=0");	
					   }else{
					   map.put("identityType", "(oc.CourseNo='lv3' or oc.CourseNo='lv4' or oc.CourseNo='lv5') and oc.instrumentType!=0");
					}
				map.put("trainType", trainType);
				map.put("kfClassType", kfClassType);
				map.put("runType", 0);
				map.put("instrumentNum1", Integer.valueOf(instrumentNum[0]));
				map.put("instrumentNum2", Integer.valueOf(instrumentNum[1]));
				map.put("orderByClause", "oc.CreateDate desc");
				List<OnlineCourse> coureseList=onlineCourseService.queryByInstrument(map);
				if(coureseList!=null&&coureseList.size()>0){//查到符合条件器械课程
					listQX.addAll(coureseList);
				}else{//未查到符合条件器械课程，减少条件查询
					Map<String, Object> map2 = new HashMap<String, Object>();
					map2.put("enabled", true);
					map2.put("isDeleted", false);
					if(identityType==376){
					   map2.put("identityType", "(oc.CourseNo='lv1' or oc.CourseNo='lv2') and oc.instrumentType!=0");
					   }else if(identityType==377){
					   map2.put("identityType", "(oc.CourseNo='lv2' or oc.CourseNo='lv3' or oc.CourseNo='lv4') and oc.instrumentType!=0");	
					   }else{
					   map2.put("identityType", "(oc.CourseNo='lv3' or oc.CourseNo='lv4' or oc.CourseNo='lv5') and oc.instrumentType!=0");
					}
					map2.put("runType", 0);
					map2.put("instrumentNum1", Integer.valueOf(instrumentNum[0]));
					map2.put("instrumentNum2", Integer.valueOf(instrumentNum[1]));
					map2.put("orderByClause", "oc.CreateDate desc");
					List<OnlineCourse> coureseList2=onlineCourseService.queryByInstrument(map2);
					listQX.addAll(coureseList2);
				}
			}else{
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("enabled", true);
				map.put("isDeleted", false);
				if(identityType==376){
					   map.put("identityType", "(oc.CourseNo='lv1' or oc.CourseNo='lv2') and oc.instrumentType!=0");
					   }else if(identityType==377){
					   map.put("identityType", "(oc.CourseNo='lv2' or oc.CourseNo='lv3' or oc.CourseNo='lv4') and oc.instrumentType!=0");	
					   }else{
					   map.put("identityType", "(oc.CourseNo='lv3' or oc.CourseNo='lv4' or oc.CourseNo='lv5') and oc.instrumentType!=0");
					}
				map.put("trainType", trainType);
				map.put("kfClassType", kfClassType);
				map.put("runType", 0);
				map.put("instrumentNum1", Integer.valueOf(instrumentNum[0]));
				map.put("instrumentNum2", Integer.valueOf(instrumentNum[1]));
				map.put("instrumentNum3", Integer.valueOf(instrumentNum[2]));
				map.put("orderByClause", "oc.CourseNo,oc.CreateDate desc");
				List<OnlineCourse> coureseList=onlineCourseService.queryByInstrument(map);
				if(coureseList!=null&&coureseList.size()>0){//查到符合条件器械课程
					listQX.addAll(coureseList);
				}else{//未查到符合条件器械课程，减少条件查询
					Map<String, Object> map2 = new HashMap<String, Object>();
					map2.put("enabled", true);
					map2.put("isDeleted", false);
					if(identityType==376){
					   map2.put("identityType", "(oc.CourseNo='lv1' or oc.CourseNo='lv2') and oc.instrumentType!=0");
					   }else if(identityType==377){
					   map2.put("identityType", "(oc.CourseNo='lv2' or oc.CourseNo='lv3' or oc.CourseNo='lv4') and oc.instrumentType!=0");	
					   }else{
					   map2.put("identityType", "(oc.CourseNo='lv3' or oc.CourseNo='lv4' or oc.CourseNo='lv5') and oc.instrumentType!=0");
					}
					map2.put("runType", 0);
					map2.put("instrumentNum1", Integer.valueOf(instrumentNum[0]));
					map2.put("instrumentNum2", Integer.valueOf(instrumentNum[1]));
					map2.put("instrumentNum3", Integer.valueOf(instrumentNum[2]));
					map2.put("orderByClause", "oc.CreateDate desc");
					List<OnlineCourse> coureseList2=onlineCourseService.queryByInstrument(map2);
					listQX.addAll(coureseList2);
				}
			}
		}
		return listQX;
	}
	
	/**
	 * 获得训练跑课程
	 * @param runType
	 * @param identityType
	 * @param trainType
	 * @param kfClassType
	 * @return
	 */
	private List<OnlineCourse> gainTrain(Integer runType,Integer identityType,Integer trainType,Integer kfClassType){
		//训练课程
		List<OnlineCourse> listRun=new ArrayList<OnlineCourse>();
		//查询包含训练跑的课程
		if(runType==2){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("enabled", true);
			map.put("isDeleted", false);
			if(identityType==376){
				   map.put("identityType", "(oc.CourseNo='lv1' or oc.CourseNo='lv2') and oc.instrumentType=0");
				   }else if(identityType==377){
				   map.put("identityType", "(oc.CourseNo='lv2' or oc.CourseNo='lv3' or oc.CourseNo='lv4') and  oc.instrumentType=0");	
				   }else{
				   map.put("identityType", "(oc.CourseNo='lv3' or oc.CourseNo='lv4' or oc.CourseNo='lv5') and  oc.instrumentType=0");
			}
			map.put("trainType", trainType);
			map.put("kfClassType", kfClassType);
			map.put("orderByClause", "oc.CourseNo,oc.CreateDate desc");
			List<OnlineCourse> coureseList=onlineCourseService.queryByRun(map);
			listRun.addAll(coureseList);
		}
		return listRun;
	}
	
	/**
	 * 获得满足三个条件的普通课程
	 * @param runType
	 * @param identityType
	 * @param trainType
	 * @param kfClassType
	 * @return
	 */
	private List<OnlineCourse> gainCommon(Integer identityType,Integer trainType,Integer kfClassType){
		//普通课程
		List<OnlineCourse> listCommon=new ArrayList<OnlineCourse>();
		Map<String, Object> reqmap = new HashMap<String, Object>();
		reqmap.put("enabled", true);
		reqmap.put("isDeleted", false);
		if(identityType==376){
			reqmap.put("identityType", "(oc.CourseNo='lv1' or oc.CourseNo='lv2') and oc.instrumentType=0");
		 }else if(identityType==377){
		    reqmap.put("identityType", "(oc.CourseNo='lv2' or oc.CourseNo='lv3' or oc.CourseNo='lv4') and oc.instrumentType=0");	
		 }else{
		    reqmap.put("identityType", "(oc.CourseNo='lv3' or oc.CourseNo='lv4' or oc.CourseNo='lv5') and oc.instrumentType=0");
		 }
		reqmap.put("trainType", trainType);
		reqmap.put("kfClassType", kfClassType);
		reqmap.put("orderByClause", "oc.CourseNo,oc.CreateDate desc");
		List<OnlineCourse> coureseList1=onlineCourseService.queryByCommon(reqmap);
		if(coureseList1!=null&&coureseList1.size()>0){
			listCommon.addAll(coureseList1);
		}else{
			Map<String, Object> reqmap2 = new HashMap<String, Object>();
			reqmap2.put("enabled", true);
			reqmap2.put("isDeleted", false);
			if(identityType==376){
				reqmap2.put("identityType", "(oc.CourseNo='lv1' or oc.CourseNo='lv2') and oc.instrumentType=0");
				reqmap2.put("trainType", trainType);
				reqmap2.put("kfClassType", 392);
			}else if(identityType==377){
				reqmap2.put("identityType", "(oc.CourseNo='lv2' or oc.CourseNo='lv3' or oc.CourseNo='lv4') and oc.instrumentType=0");	
				reqmap2.put("trainType", 384);
				reqmap2.put("kfClassType", kfClassType);
			}else{
				reqmap2.put("identityType", "(oc.CourseNo='lv3' or oc.CourseNo='lv4' or oc.CourseNo='lv5') and oc.instrumentType=0");
				reqmap2.put("trainType", 384);
				reqmap2.put("kfClassType", kfClassType);
			}
			reqmap2.put("orderByClause", "oc.CourseNo,oc.CreateDate desc");
			List<OnlineCourse> coureseList2=onlineCourseService.queryByCommon(reqmap2);
			listCommon.addAll(coureseList2);
		}
		
		return listCommon;
	}

}
